<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>编辑菜单</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <link rel="stylesheet" href="/static/admin/lib/layui-v2.5.5/css/layui.css" media="all">
    <link rel="stylesheet" href="/static/admin/lib/font-awesome-4.7.0/css/font-awesome.min.css" media="all">
    <link rel="stylesheet" href="/static/admin/css/public.css" media="all">
    <style>
        .layui-iconpicker-body.layui-iconpicker-body-page .hide {
            display: none;
        }
    </style>
</head>
<body>
<div class="layuimini-container">
    <div class="layuimini-main">

        <form class="layui-form layuimini-form" action="" lay-filter="edit">
            <div class="layui-form-item">
                <label class="layui-form-label">上级菜单</label>
                <div class="layui-input-inline">
                    <select name="pid" lay-verify="required" lay-search="">
                        <option value="0">顶部菜单</option>
                        ${range $k,$v := .menu_list}
                        <option value="${$v.ID}">${$v.Title}</option>
                        ${end}
                    </select>
                </div>
                <div class="layui-form-mid layui-word-aux">必选，请选择上级菜单或顶级菜单</div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label required">菜单名称</label>
                <div class="layui-input-inline">
                    <input type="text" name="title" lay-verify="required" lay-reqtext="菜单名称是必填项"
                           placeholder="请输入" autocomplete="off" class="layui-input">
                </div>
            </div>
            <div class="layui-form-item">
                <label for="" class="layui-form-label">菜单图标</label>
                <div class="layui-input-block">
                    <input type="text" id="iconPicker" name="icon" lay-filter="iconPicker" class="hide">
                </div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label">菜单路由</label>
                <div class="layui-input-block">
                    <select name="route" lay-search="">
                        <option value=""></option>
                        ${range $k,$v := .route}
                        <option value="${$v.Value}">${$v.Label}</option>
                        ${end}
                    </select>
                </div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label">跳转类型</label>
                <div class="layui-input-block">
                    <input type="radio" name="target" value="_self" title="_self" checked="">
                    <input type="radio" name="target" value="_top" title="_top">
                    <input type="radio" name="target" value="_parent" title="_parent">
                    <input type="radio" name="target" value="_blank" title="_blank">
                </div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label">状态</label>
                <div class="layui-input-block">
                    <input type="radio" name="status" value="1" title="启用" checked="">
                    <input type="radio" name="status" value="0" title="禁用">
                </div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label">排序</label>
                <div class="layui-input-inline">
                    <input type="text" name="sort" lay-verify="" value="0" autocomplete="off"
                           class="layui-input">
                </div>
            </div>
            <div class="layui-form-item layui-form-text">
                <label class="layui-form-label">备注</label>
                <div class="layui-input-block">
                    <textarea placeholder="请输入内容" name="remark" class="layui-textarea"></textarea>
                </div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label">是否按钮</label>
                <div class="layui-input-block">
                    <input type="checkbox" name="is_menu" lay-skin="switch" lay-filter="switchMenu" lay-text="是|否">
                </div>
                <div class="layui-form-mid layui-word-aux">顶层固定为目录,非按钮为菜单</div>
            </div>
            <div class="layui-form-item">
                <div class="layui-input-block">
                    <button class="layui-btn" lay-submit="" lay-filter="submit">立即提交</button>
                    <button type="reset" class="layui-btn layui-btn-primary">重置</button>
                </div>
            </div>
        </form>
    </div>
</div>

<script src="/static/admin/lib/layui-v2.5.5/layui.js" charset="utf-8"></script>
<script src="/static/admin/js/lay-config.js?v=1.0.4" charset="utf-8"></script>
<script>
    layui.use(['iconPickerFa', 'form', 'layer'], function () {
        var iconPickerFa = layui.iconPickerFa,
            form = layui.form,
            layer = layui.layer,
            $ = layui.$;

        iconPickerFa.render({
            // 选择器，推荐使用input
            elem: '#iconPicker',
            // fa 图标接口
            url: "/static/admin/lib/font-awesome-4.7.0/less/variables.less",
            // 是否开启搜索：true/false，默认true
            search: true,
            // 是否开启分页：true/false，默认true
            page: true,
            // 每页显示数量，默认12
            limit: 50,
            // 点击回调
            click: function (data) {
                document.getElementById("iconPicker").value = "fa " + data.icon
            },
            // 渲染成功后的回调
            success: function (d) {
                iconPickerFa.checkIcon('iconPicker', "${.info.Icon}");
            }
        });
        //监听指定开关
        form.on('switch(switchMenu)', function (data) {
            if (this.checked) {
                this.value = 1
            } else {
                this.value = 0
            }
        });
        //监听提交
        form.on('submit(submit)', function (data) {
            data.field.is_menu = data.field.is_menu ? 1 : 0
            let id = "${.info.Id}"
            let method = 'post'
            if (id > 0) {
                method = 'put'
                data.field.id = id
            }
            $.ajax({
                url: "/api/system/menu",
                type: method,
                data: data.field,
                success: function (res) {
                    layer.msg(res.msg);
                    if (res.code === 0) {
                        parent.location.reload(); //成功后再刷新
                    }
                },
                error: function (res) {
                    layer.msg(res.msg);
                    return false
                }
            })
            return false;
        });
        //表单初始赋值
        form.val('edit', {
            "pid": "${.info.Pid}"
            , "title": "${.info.Title}"
            , "route": "${.info_route}"
            , "target": "${.info.Target}"
            , "status": "${.info.Status}"
            , "is_menu": "${if .info.IsMenu}true${end}"
            , "sort": "${.info.Sort}"
            , "remark": "${.info.Remark}"
        })
    });
</script>

</body>
</html>